package ui.panels.additionalSettings;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;

import ui.flowManager.FlowManagerImpl;
import ui.flowManager.expirimentProperties.AdditionalSettings;
import ui.flowManager.expirimentProperties.ExperimentProperties;
import ui.panels.commands.SetEnablecommand;
import ui.panels.primary.adaptiveAgentProperties.AdaptiveAgentProperties;
import ui.panels.primary.playerProperties.PlayerPropertiesPanel_2;


/**
 * @author vladimir
 * 
 * This is the panel for additional settings of adaptive player
 */
public class AdditionalSettingsPanel extends JPanel{

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	private JCheckBox 				stopAutomatically = null;
	private JCheckBox 				doLog = null;
	private JButton 				okButton = null;
	private JButton 				cancelButton = null;	
	private AdditionalSettingsFrame myFrame = null;
	private JPanel 					propertiesPanel = null;
	private AdditionalSettings 		additionalSettings = null;
	
	//private int 					playerNumber = 0;
	//private int 					gameNumber = 0;
	

	public AdditionalSettingsPanel(AdditionalSettingsFrame frame, 
			JPanel panel, AdditionalSettings additionalSettings)
	{
		this.myFrame = frame;
		this.additionalSettings = additionalSettings;
		
		if (panel instanceof PlayerPropertiesPanel_2)
			this.propertiesPanel = (PlayerPropertiesPanel_2)panel;
		else if (panel instanceof AdaptiveAgentProperties)
			this.propertiesPanel = (AdaptiveAgentProperties)panel;
		initGUI();
	}
	
	/**
	 * 
	 */
	private void initGUI()
	{
	
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			setPreferredSize(new Dimension(400, 300));
			{
				doLog = new JCheckBox("           Write log to this Agent");
				doLog.setFont(new Font(Font.SANS_SERIF, Font.CENTER_BASELINE, 15));
			}
			{
				stopAutomatically = new JCheckBox();
				stopAutomatically.setText("          Stop Running Automatically");
				stopAutomatically.setFont(new Font(Font.SANS_SERIF,Font.CENTER_BASELINE,15));
			}
			{
				okButton = new JButton();
				okButton.setText("OK");
			}
			{
				cancelButton = new JButton("Cancel");
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(44, 44)
				.addComponent(doLog, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addGap(36)
				.addComponent(stopAutomatically, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
				.addGap(107)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(cancelButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
				    .addComponent(okButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE))
				.addContainerGap(39, 39));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(42, 42)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(doLog, GroupLayout.PREFERRED_SIZE, 273, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 14, Short.MAX_VALUE))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(stopAutomatically, GroupLayout.PREFERRED_SIZE, 273, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 14, Short.MAX_VALUE))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGap(39)
				        .addComponent(okButton, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
				        .addGap(74)
				        .addComponent(cancelButton, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE)))
				.addContainerGap(71, 71));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//add listeners too the buttons 
		setListeners();
		//set the GUI if all ready initialized
		setAdditionalSettings();
	}
	
	public JButton getOkButton() {
		return okButton;
	}

	public JButton getCancelButton() {
		return cancelButton;
	}


	
	/**
	 * functionality when OK clicked
	 * @param props
	 */
	public void okButtonFunc()
	{
		//case to do log for the player
		additionalSettings.setLog(doLog.isSelected());
		//case the player will stop automatically
		additionalSettings.setStopAutomatically(stopAutomatically.isSelected());
	}
	
	/**
	 * Enable the prime GUI && close this panel frame
	 */
	public void setEnablePrimePanelAndClose()
	{
		//set enable the prime GUI , use via command pattern
		SetEnablecommand enableCommand = new SetEnablecommand();
		enableCommand.setArg(true);
		enableCommand.execute();
		//close the manual panel
		myFrame.dispose();
	}
	
	private void setListeners()
	{
		//take the experiments properties
		ExperimentProperties props = FlowManagerImpl.getInstance().getProps();
		
		//set the buttons
		AdditionalSettingsListener actionListener  = new AdditionalSettingsListener(this, props);
		okButton.addActionListener(actionListener);
		cancelButton.addActionListener(actionListener);		
	}
	
	
	private void setAdditionalSettings()
	{
		this.stopAutomatically.setSelected(additionalSettings.isStopAutomatically());
		this.doLog.setSelected(additionalSettings.isLog());
	}
}
